<template>
  <div class="todoapp">
    <TodoHeader :arr="newArr" @check="checkFn" @addlist="addFn"></TodoHeader>
    <TodoMain @dellist="delFn" :arr="newArr"></TodoMain>
    <TodoFooter
      @clearlist="clearFn"
      @change="changeFn"
      :arr="newArr"
    ></TodoFooter>
  </div>
</template>

<script>
import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain";
import TodoFooter from "./components/TodoFooter";
export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  data() {
    return {
      list: JSON.parse(localStorage.getItem("list")) || [],
      str: "all",
    };
  },
  methods: {
    addFn(name) {
      this.list.push({
        id:
          this.list.length == 0 ? 100 : this.list[this.list.length - 1].id + 1,
        name,
        isDone: false,
      });
    },
    delFn(tId) {
      // console.log(tId);
      // console.log(this.list.findIndex((obj) => obj.id == tId));
      this.list.splice(
        this.list.findIndex((obj) => obj.id == tId),
        1
      );
    },
    checkFn(val) {
      this.list.forEach((obj) => (obj.isDone = val));
    },
    changeFn(str) {
      this.str = str;
    },
    clearFn() {
      this.list = this.list.filter((obj) => obj.isDone === false);
    },
  },
  computed: {
    newArr() {
      if (this.str === "isCom") {
        return this.list.filter((obj) => obj.isDone == false);
      } else if (this.str === "com") {
        return this.list.filter((obj) => obj.isDone == true);
      } else {
        return this.list;
      }
    },
  },
  watch: {
    list: {
      handler(newVal) {
        localStorage.setItem("list", JSON.stringify(newVal));
      },
      deep: true,
    },
  },
};
</script>

<style>
</style>